package com.linkstec.jexl;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.MapContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;

public class Jexl3Test {
	private static final Logger logger = LogManager.getLogger();

	public static void main(String[] args) {
//		String ex = "false & false | false";
		String ex = "(a > 1 & true ) | b <= 4";
		Map<String, Object> map = new HashMap<String, Object>();
		
//		map.put("a", "1");
		map.put("b", "4");
		JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(map);
//		JexlContext jc = new MapContext(map);
		JexlContext jc = new MapContext(jsonObject1);
		
//		jc.set("a", "2.2");//java.lang.NumberFormatException: For input string: "2.2"
//		jc.set("a", "2");
//		jc.set("b", 2);
		long start = System.currentTimeMillis();
		boolean bbb = false;
//		int j = 1000000;
		int j = 1000;
		for (int i = 0; i < j; i++){
			bbb = parseBoolean(ex, jc);
		}
		long end = System.currentTimeMillis();
		logger.info((end - start));
		if (bbb) {
			logger.info("true");
		} else {
			logger.info("false");
		}
//		test();

	}
	public static boolean parseBoolean(String ex) {
		JexlExpression e = new JexlBuilder().create().createExpression(ex);
		logger.info(e.evaluate(null));
		long i = (long) e.evaluate(null);
		if (i ==1) {
			return true;
		} else {
			return false;
		}
	}
	public static boolean parseBoolean(String ex, JexlContext jc) {
		JexlExpression e = new JexlBuilder().create().createExpression(ex);
		Object result = e.evaluate(jc);
		if (result instanceof Boolean) {
		    return (Boolean) result;
		}
		long i = (long) result;
		if (i ==1) {
			return true;
		} else {
			return false;
		}
	}
	public static void loop() {
	    JexlContext jc = new MapContext();
	    jc.set("a", 1);
	    jc.set("b", "0");
	    jc.set("ans", new StringBuffer());
	    JexlExpression e = new JexlBuilder().create().createExpression("while (a < 10) {a = a + 1;ans.append(b);}");
	    e.evaluate(jc);
	    System.out.println(jc.get("ans"));
	}
	public static void test() {
		JexlContext jc = new MapContext();
		jc.set("a", 1);
		jc.set("b", "10");
		jc.set("ans", new StringBuffer());
//		Expression e = new JexlEngine().createExpression("ans = a + b");
		JexlExpression e = new JexlBuilder().create().createExpression("a + b");
		int result = (int) e.evaluate(jc);
		System.out.println(jc.get("ans"));
		logger.info("result值为："+ result);
	}
	public static void getSet() {
	    TmpTest tmpTest = new TmpTest();
	    tmpTest.setA(1);
	    JexlContext jc = new MapContext();
	    jc.set("tmpTest", tmpTest);
	    jc.set("ans", "");
	    JexlExpression e = new JexlBuilder().create().createExpression("ans = tmpTest.getA()");
	    e.evaluate(jc);
	    System.out.println(jc.get("ans"));
	    e = new JexlBuilder().create().createExpression("ans = tmpTest.setA(2)");
	    e.evaluate(jc);
	    TmpTest tmpTest1 = (TmpTest) jc.get("tmpTest");
	    System.out.println(tmpTest1.getA());
	}
	public static boolean regMatch(String regEx, String str) {
        Pattern pattern = Pattern.compile(regEx);
        return pattern.matcher(str).matches();
    }
}